ControlP5 controlP5;

Textarea fileDataT;
DropdownList comPorts;

int bgColor = #252828;

iButton Abrir;
iButton Atualizar;
iButton Editar;
iButton Sair;
iButton Config;
iButton Connection;

PImage mbar;
PImage drillsMapImg;
PFont dfont = createFont("FootlightMTLight", 16, true);

void iSetup() {
  controlP5 = new ControlP5(this);
  mbar = loadImage("mbar.png");
  fileDataT = controlP5.addTextarea("FileDataLabel", "", 780, 55, 200, 400);
  fileDataT.setColor(0);
  fileDataT.setColorBackground(255);
  comPorts = controlP5.addDropdownList("ComPorts",540,35,120,120);
  comPorts.setItemHeight(20);
  comPorts.setBarHeight(20);
  comPorts.captionLabel().style().marginTop = 5;
  comPorts.captionLabel().style().marginLeft = 3;
  comPorts.setColorBackground(255);
  comPorts.setColorForeground(color(220,220,255));
  comPorts.setColorLabel(0);
  for(int i = 0; i < serialPort.list().length ; i++){
    comPorts.addItem(serialPort.list()[i],i);
  }
  Abrir = new iButton("abrir1.png", "abrir2.png", 10, 5, "Abrir");
  Editar = new iButton("edi1.png", "edi2.png", 60, 5, "Editar");
  Atualizar = new iButton("ref1.png", "ref2.png", 110, 5, "Atualizar");
  Config = new iButton("cnf1.png", "cnf2.png", 170, 5, "Atualizar");
  Connection = new iButton("dconn.png",680,5,"Connect");
  Sair = new iButton("qt2.png", "qt1.png", 950, 5, "Sair");


  textFont(dfont);
}
void iDraw() {
  background(bgColor);
  image(mbar, 0, 0);
  iviewerDraw();
  iButtonDraws();
  
}
//////////////////////////////////////////////////
//Ibuttons Clicked
//////////////////////////////////////////////////
void mouseClicked() {
  if (Abrir.mouseOver()) {
    filePath = selectInput("");
    fileRead(filePath);
  }
  if (Sair.mouseOver()) {
    exit();
  }
}

void iButtonDraws() {
  Abrir.Draw();
  Editar.Draw();
  Sair.Draw();
  Atualizar.Draw();
  Config.Draw();
  Connection.Draw();
}
//////////////////////////////////////////////////
//control API functions
/////////////////////////////////////////////////
public void Abrir() {
  filePath = selectInput("");
  fileRead(filePath);
}

public void Sair() {
  exit();
}

public void Sobre() {
  link("http://www.processing.org");
}
///////////////////////////////////////////////////
//Job Image Viewer
//////////////////////////////////////////////////
int iviewer_x = 200;
int iviewer_y = 55;
int iviewer_w =575;
int iviewer_h = 400;
int iviewer_c = 0;

void iviewerDraw() {
  fill(iviewer_c);
  rect(iviewer_x, iviewer_y, iviewer_w, iviewer_h);
  fill(255, 0, 0);
  stroke(255, 0, 0);
  strokeWeight(1);
  line(iviewer_x+10, iviewer_y+iviewer_h-10, iviewer_x+70, iviewer_y+iviewer_h-10);
  line(iviewer_x+10, iviewer_y+iviewer_h-10, iviewer_x+10, iviewer_y+iviewer_h-70);
  triangle(iviewer_x+70, iviewer_y+iviewer_h-15, iviewer_x+80, iviewer_y+iviewer_h-10, iviewer_x+70, iviewer_y+iviewer_h-5);
  triangle(iviewer_x+15, iviewer_y+iviewer_h-70, iviewer_x+10, iviewer_y+iviewer_h-80, iviewer_x+5, iviewer_y+iviewer_h-70);
  if (drillsMapImg != null) {
    imageMode(CENTER);
    image(drillsMapImg, iviewer_x+iviewer_w/2, (iviewer_y+iviewer_h/2), drillsMapImg.width, -drillsMapImg.height);
    imageMode(CORNER);
  }
}

void formDrillsImage() {
  ArrayList dots = new ArrayList();
  dots.add(loadImage("pt0.png"));
  dots.add(loadImage("pt1.png"));
  dots.add(loadImage("pt2.png"));
  int h =0;
  int w =0;
  int sRate = 1;
  for (int t=0; t<tools.size();t++) {
    Tool ptld = (Tool) tools.get(t);
    for (int i=0; i < ptld.drills.size();i++) {
      Drill ptd = (Drill) ptld.drills.get(i);
      if (ptd.x>w) w = ptd.x;
      if (ptd.y>h) h = ptd.y;
    }
  }
  while ( (w/sRate > iviewer_w-19)||(h/sRate >iviewer_h-19)) {
    sRate++;
  }
  if (drillsMapImg == null) 
  drillsMapImg = createImage(w/sRate+19, h/sRate+19, ARGB);
  else {
    drillsMapImg = new PImage();
    drillsMapImg = createImage(w/sRate+19, h/sRate+19, ARGB);

  }
  drillsMapImg.loadPixels();

  for (int t=0; t<tools.size();t++) {
    Tool ptld = (Tool) tools.get(t);
    for (int i=0; i < ptld.drills.size();i++) {
      Drill ptd = (Drill) ptld.drills.get(i);
      drillsMapImg.set(int(ptd.x/sRate), int(ptd.y/sRate), (PImage) dots.get(t));
    }
  }
  drillsMapImg =   mirrorY(drillsMapImg);
  drillsMapImg.updatePixels();
  //drillsMapImg.resize(drillsMapImg.width,-drillsMapImg.height);
}

PImage mirrorY(PImage img)
{
  PImage mirror;
  img.loadPixels();
  int cols = img.width;
  int rows = img.height;
  mirror = createImage(cols, rows, ARGB);
  // Begin loop for columns
  for (int i = 0; i < rows ; i++)
  {
    // Begin loop for rows
    for (int j = 0; j < cols; j++)
    {

      // makes a new index for the pixels
      int location = j + i*cols;
      //set the pixels in the image to their new values
      mirror.set(j, rows-i, img.pixels[location]);
    }
  }
  mirror.updatePixels();
  return mirror;
}


